package com.p7.notepad.app;

import android.app.Application;
import android.content.res.Resources;
import android.support.v7.app.AppCompatDelegate;
import android.widget.ImageView;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.p7.notepad.R;
import com.p7.notepad.tools.Storage;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.io.File;

public class MyApplication extends Application {

    private static Resources sRes;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        sRes = this.getResources();
        SpeechUtility.createUtility(this, SpeechConstant.APPID + "=5cdccc3f");
    }

    private static MyApplication instance;
    public static MyApplication getInstance() {
        return instance;
    }

    public void setImage(String url, ImageView iv) {
        Picasso.with(getInstance())
                .load(url)
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .fit()
                .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                .into(iv);
    }

    public void setImage(File url, ImageView iv) {
        Picasso.with(getInstance())
                .load(url)
                .into(iv);
    }

    /**
     * 切换 夜间模式
     * @param on true 夜间， false  日间
     */
    public void updateNightMode(boolean on) {
        Storage.putMode(on);
//        DisplayMetrics dm = sRes.getDisplayMetrics();
//        Configuration config = sRes.getConfiguration();
//        config.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
//        config.uiMode |= on ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO;
//        sRes.updateConfiguration(config, dm);

        if (on) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
    }
}
